package com.zxy.ziems.server.production.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.zxy.ziems.server.mybatis.core.service.ServiceImplX;
import com.zxy.ziems.server.production.mapper.CustXyhProsalestoreMapper;
import com.zxy.ziems.server.production.mapper.CustXyhViewMapper;
import com.zxy.ziems.server.production.pojo.entity.CustXyhProsalestoreEntity;
import com.zxy.ziems.server.production.pojo.param.CustXyhProsalestoreParam;
import com.zxy.ziems.server.production.pojo.vo.CustXyhProjectNameVo;
import com.zxy.ziems.server.production.pojo.vo.CustXyhProsalestoreTotalVO;
import com.zxy.ziems.server.production.pojo.vo.CustXyhProsalestoreVo;
import com.zxy.ziems.server.production.service.CustXyhProsalestoreService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Copyright (C),
 * @ClassName: CustXyhProsalestoreServiceImpl
 * @Author: yanzhiwei
 * @Description:
 * @Date: 2024/5/11 10:34
 */
@Service
public class CustXyhProsalestoreServiceImpl extends ServiceImplX<CustXyhProsalestoreMapper, CustXyhProsalestoreEntity>
        implements CustXyhProsalestoreService {
    @Autowired
    private CustXyhProsalestoreMapper custXyhProsalestoreMapper;

    @Autowired
    private CustXyhViewMapper custXyhViewMapper;

    @Override
    public List<CustXyhProsalestoreVo> getList(CustXyhProsalestoreParam param) {
        //搜索时间为当天时，查询昨天的数据
        if (LocalDate.now().equals(param.getDate())) {
            param.setDate(LocalDate.now().minusDays(1L));
        }
        LambdaQueryWrapper<CustXyhProsalestoreEntity> wrapper = new LambdaQueryWrapper();
        wrapper.eq(CustXyhProsalestoreEntity::getCreateDate, param.getDate())
                .in(CollectionUtil.isNotEmpty(param.getProjectCodeList()),
                        CustXyhProsalestoreEntity::getProjectNo, param.getProjectCodeList());
        List<CustXyhProsalestoreEntity> custXyhProsalestoreEntityList = this.list(wrapper);


        final List<CustXyhProsalestoreVo> custXyhProsalestoreVoList = custXyhProsalestoreEntityList.stream().map(m -> {
            CustXyhProsalestoreVo custXyhProsalestoreVo = new CustXyhProsalestoreVo();
            BeanUtils.copyProperties(m, custXyhProsalestoreVo);
            custXyhProsalestoreVo.setSalesPlanCurMonth(kgToT(custXyhProsalestoreVo.getSalesPlanCurMonth()));
            custXyhProsalestoreVo.setSalesPlanCurAddOne(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddOne()));
            custXyhProsalestoreVo.setSalesPlanCurAddTwo(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddTwo()));
            custXyhProsalestoreVo.setSalesPlanCurAddThree(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddThree()));
            custXyhProsalestoreVo.setSalesPlanCurAddFour(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddFour()));
            custXyhProsalestoreVo.setSalesPlanCurAddFive(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddFive()));
            custXyhProsalestoreVo.setSalesPlanCurAddSix(kgToT(custXyhProsalestoreVo.getSalesPlanCurAddSix()));
            custXyhProsalestoreVo.setSalesPlanTotal(
                    custXyhProsalestoreVo.getSalesPlanCurMonth()
                            .add(custXyhProsalestoreVo.getSalesPlanCurAddOne())
                            .add(custXyhProsalestoreVo.getSalesPlanCurAddTwo())
                            .add(custXyhProsalestoreVo.getSalesPlanCurAddThree())
            );

            custXyhProsalestoreVo.setFinishedInventory(kgToT(custXyhProsalestoreVo.getFinishedInventory()));
            custXyhProsalestoreVo.setMaterialInventory(kgToT(custXyhProsalestoreVo.getMaterialInventory()));
            custXyhProsalestoreVo.setInventoryTotal(kgToT(custXyhProsalestoreVo.getInventoryTotal()));

            custXyhProsalestoreVo.setPurchasedMaterialCurMonth(kgToT(custXyhProsalestoreVo.getPurchasedMaterialCurMonth()));
            custXyhProsalestoreVo.setPurchasedMaterialCurAddOne(kgToT(custXyhProsalestoreVo.getPurchasedMaterialCurAddOne()));
            custXyhProsalestoreVo.setPurchasedMaterialCurAddTwo(kgToT(custXyhProsalestoreVo.getPurchasedMaterialCurAddTwo()));
            custXyhProsalestoreVo.setPurchasedMaterialCurAddThree(kgToT(custXyhProsalestoreVo.getPurchasedMaterialCurAddThree()));
            custXyhProsalestoreVo.setTotal(kgToT(custXyhProsalestoreVo.getTotal()));
            return custXyhProsalestoreVo;
        }).collect(Collectors.toList());


        return custXyhProsalestoreVoList;
    }


    /**
     * 千克转吨
     *
     * @param kg
     * @return
     */
    private BigDecimal kgToT(BigDecimal kg) {
        if (kg == null) {
            return null;
        }
        return kg.divide(new BigDecimal(1000), 2, RoundingMode.HALF_UP);
    }

    @Override
    public List<CustXyhProjectNameVo> projectNameList(LocalDate date) {
        //搜索时间为当天时，查询昨天的数据
        if (LocalDate.now().equals(date)) {
            date = LocalDate.now().minusDays(1L);
        }

        List<CustXyhProjectNameVo> custXyhProjectNameVoList = custXyhProsalestoreMapper.projectNameList(date);

        return custXyhProjectNameVoList;
    }

    @Override
    public void synData() {
        LocalDate crateDate = LocalDate.now().minusDays(1l);
        List<CustXyhProsalestoreEntity> data = custXyhViewMapper.listProsalestoreView(LocalDate.now());
        data.stream().forEach(x -> {
            x.setCreateDate(crateDate);
            x.setId(IdWorker.getIdStr());
        });
        saveOrUpdateBatch(data);
    }

    @Override
    public CustXyhProsalestoreTotalVO total() {
        CustXyhProsalestoreTotalVO custXyhProsalestoreTotalVO = baseMapper.total();
        custXyhProsalestoreTotalVO.setFinishedInventory(kgToT(custXyhProsalestoreTotalVO.getFinishedInventory()));
        custXyhProsalestoreTotalVO.setMaterialInventory(kgToT(custXyhProsalestoreTotalVO.getMaterialInventory()));
        custXyhProsalestoreTotalVO.setTotal(kgToT(custXyhProsalestoreTotalVO.getTotal()));
        return custXyhProsalestoreTotalVO;
    }


}
